class Graph{
    constructor(isDirected = false){
        this.isDirected = isDirected;//无向和有向
        this.vertices = [];//存放顶点
        this.edge = new Map();//邻接点
    }
    //添加顶点
    addVertex(v){
        if(!this.vertices.includes(v)){
            this.vertices.push(v);
            this.edge.set(v,[]);
        }
    }
    //添加边
    addEdge(v,w){
        if(!this.vertices.includes(v)){
            this.addVertex(v)
        }
        if(!this.vertices.includes(vw)){
            this.addVertex(w)
        }

        this.edge.get(v).push(w)
        if(!this.isDirected){//无向
            this.edge.get(w).push(v)
        }
    }
    //获取顶点
    getVertices(){
        return this.vertices;
    }
    //
    getEdge(){
        return this.edge
    }
    toString(){
        let str = '';
        for(let i = 0 ; i <this.vertices.length ; i++){
            str += `${this.vertices[i]}--->`
            let edgeList = this.edge.get(this.vertices[i])
            for(let j = 0 ; j < edgeList.length ; j++ ){
                str += `${list[j]}`
            }
            str+='\n'
        }
        return str
    }
}
let g = new Graph()

g.addVertex('A')
g.addVertex('B')
console.log(g)